package com.fulihui.wgj.board.websocket.filter;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.fulihui.wgj.board.websocket.message.LoggerMessage;
import com.fulihui.wgj.board.websocket.queue.LoggerQueue;

import java.text.DateFormat;
import java.util.Date;

/**
 * @author: Levon
 * @date: 2018-07-12 16:42
 */
public class LogFilter extends Filter {

    @Override
    public FilterReply decide(Object o) {

        if (o instanceof ILoggingEvent) {

            ILoggingEvent event = (ILoggingEvent) o;

            LoggerMessage loggerMessage = new LoggerMessage(
                    event.getMessage(),
                    DateFormat.getDateTimeInstance().format(new Date(event.getTimeStamp())),
                    event.getThreadName(),
                    event.getLoggerName(),
                    event.getLevel().levelStr
            );
            LoggerQueue.getInstance().push(loggerMessage);
        }
        return FilterReply.ACCEPT;
    }
}
